package com.li.youhappygo.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.annotation.Log;
import com.li.youhappygo.annotation.OperatorType;
import com.li.youhappygo.model.dto.OrderQueryParamDto;
import com.li.youhappygo.model.vo.common.Result;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.model.vo.order.OrderDetailVo;
import com.li.youhappygo.model.vo.order.OrderVo;
import com.li.youhappygo.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/order")
@Tag(name = "订单管理", description = "订单管理")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("getAllOrder/{pageNum}/{pageSize}")
    @Operation(summary = "查询所有订单的简略信息",description = "直接查询所有的订单")
    public Result<PageInfo<OrderVo>> getAllOrder(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        List<OrderVo> orderVoList = orderService.getAllOrder(pageNum, pageSize);
        PageInfo<OrderVo> pageInfo = new PageInfo(orderVoList);
        List<OrderVo> res = pageInfo.getList();
        PageInfo<OrderVo> pageInfo1 = new PageInfo(res);
        return Result.build(pageInfo1, ResultCode.SUCCESS);
    }

    @GetMapping("getDetailInfoByOrderId/{orderId}")
    @Operation(summary = "根据订单ID查询订单详细信息",description = "根据订单ID查询订单详细信息")
    public Result<OrderDetailVo> getDetailInfoByOrderId(@PathVariable Long orderId) {
        OrderDetailVo orderDetailVo= orderService.getDetailInfoByOrderId2(orderId);
        return Result.build(orderDetailVo, ResultCode.SUCCESS);
    }


    @GetMapping("getDetailInfo/{pageNum}/{pageSize}")
    @Operation(summary = "根据订单ID查询订单详细信息",description = "根据订单ID查询订单详细信息")
    public Result<PageInfo<OrderDetailVo>> getDetailInfoByOrderId(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
            List<OrderDetailVo> orderDetailVo= orderService.getDetailInfoByOrderId(pageNum,pageSize);
            PageInfo<OrderDetailVo> pageInfo = new PageInfo(orderDetailVo);
            return Result.build(pageInfo, ResultCode.SUCCESS);
        }


    @GetMapping("getOrderByStatus/{status}/{pageNum}/{pageSize}")
    @Operation(summary = "根据订单状态查找订单列表")
    public Result<List<OrderVo>> getOrderByStatus(@PathVariable Integer status,@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        List<OrderVo> orderVoList = orderService.getOrderByStatus(status,pageNum,pageSize);
        return Result.build(orderVoList, ResultCode.SUCCESS);
    }

    @GetMapping("getOrderByOrderNum/{orderNum}")
    @Operation(summary = "根据订单编号查找订单")
    public Result<OrderVo> getOrderByOrderNum(@PathVariable String orderNum) {
        OrderVo orderVo = orderService.getOrderByOrderNum(orderNum);
        return Result.build(orderVo, ResultCode.SUCCESS);
    }

    @PostMapping("update/remark")
    @Operation(summary = "更新订单备注",description = "更新订单备注")
    @Log(title = "更新订单备注", businessType = 1,operatorType = OperatorType.MANAGE)
    public Result updateRemark(@RequestParam("orderId") Long orderId,@RequestParam("remark") String remark){
        orderService.updateRemark(orderId,remark);
        return Result.build(null, ResultCode.SUCCESS);
    }


    @PostMapping("update/orderStatus")
    @Operation(summary = "批量更新订单状态",description = "更新订单状态")
    @Log(title = "批量更新订单状态", businessType = 1,operatorType = OperatorType.MANAGE)
    public Result updateOrderStatus(@RequestBody List<Long> ids,@RequestParam("orderStatus") Integer orderStatus){
        orderService.updateOrderStatus(ids,orderStatus);
        return Result.build(null, ResultCode.SUCCESS);
    }


    @PostMapping("getOrderByQueryParam")
    @Operation(summary = "根据查询条件查找订单",description = "根据查询条件查找订单")

    public Result<PageInfo<OrderDetailVo>> getOrderByQueryParam(@RequestBody OrderQueryParamDto orderQueryParamDto, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo<OrderDetailVo> pageInfo = orderService.getOrderByQueryParam(orderQueryParamDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCode.SUCCESS);
   }

}
